package cmsi.Controller;

import cmsi.vo.CurrentUserModel;
import com.alibaba.fastjson.JSONObject;
import net.lcheng.model.PProduct;
import net.lcheng.model.User;
import net.lcheng.service.PProductService;
import net.lcheng.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Created by LV on 2016/7/14.
 */
@Controller
public class GoodsController {
    @Autowired
    private PProductService productService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/goods/getGoodsInformation",method = RequestMethod.GET)
    public String getGoodsInformation(Model model,@RequestParam("pid") String productId){
        Subject currentUser= SecurityUtils.getSubject();
        //未登录时的商品详情
        if (!currentUser.isAuthenticated()) {
            if (productId!=null) {
                //获取商品信息
                PProduct product = productService.getProductByPrimaryKey(productId);
                model.addAttribute("product", product);
                //商品的所有图片
                if (product != null) {
                    String img = product.getProductImages();
                    //将图片地址分割
//                    String[] imgs = img.split("\\|");
//                    model.addAttribute("imgs", imgs);
//                    //产品的属性
//                    List<PProductAttribute> productAttributes = productAttributeService.getProductAttributeByIdAndType(productId,1);
//                    model.addAttribute("productAttributes",productAttributes);
//                    model.addAttribute("shopId",product.getWxShopShopId());

                    model.addAttribute("cartNum",-1);

                    return "/goods/goods_information";
                }
                return "redirect:/error";
            }
            return "redirect:/error";
        }else {
            //登录时的商品详情
            /**获取当前用户*/
            Session session = currentUser.getSession();
            CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            User user = userService.getUserByUserId(currentUserModel.getUserId());
            if (productId!=null) {
                //获取商品信息
                PProduct product = productService.getProductByPrimaryKey(productId);

                model.addAttribute("product", product);
                //商品的所有图片
                if (product != null) {
                    String img = product.getProductImages();
                    //将图片地址分割
//                    String[] imgs = img.split("\\|");
//                    model.addAttribute("imgs", imgs);

                    //产品的属性
//                    List<PProductAttribute> productAttributes = productAttributeService.getProductAttributeByIdAndType(productId, 1);
//                    model.addAttribute("productAttributes", productAttributes);
//                    model.addAttribute("shopId", product.getWxShopShopId());

                    return "/goods/goods";
                }
                return "redirect:/error";
            }

        }
        return "redirect:/error";
    }
}
